#!/usr/bin/python

import os
import sys
import re
import time
from testUnit import tester as test
from mount import mount as mon

class nfs_server():

    __Excute = None

    def __init__(self):
        self.__Excute = os.system

    def install(self):
        self.__Excute("yum install -y nfs-utils")

    def run(self):
        self.__Excute("systemctl restart rpcbind.service;systemctl restart nfs-server.service")
        return True

    def config(self,ips,dev):
        if os.path.exists("/storage"):
            self.__Excute("rm -rf /storage")
        self.__Excute("mkdir /storage")
        M = mon()
        if dev['swift-device']:
            M.mount_disk("127.0.0.1",dev['swift-device'],"/storage")
#                self.mount_disk(dev['swift-device'])
        writer = open("/etc/exports","w")
        configs = "/storage"
        for ip in ips:
            configs += " "+ip+"(rw,sync,no_root_squash,no_wdelay)"
        writer.write(configs+"\n")
        writer.close()

        return

    def mount_disk(self,dev):
        reader = os.popen("parted -s "+dev+" print")
        size = None
        data = reader.readlines()
        reader.close()
        for d in data:
            if re.match(r"Disk /dev/.*",d):
                x = re.search(r"Disk /dev/\w+: (\d+\.\d+)GB",d)
                size = x.group(1)
        self.__Excute("parted -s "+dev+" mklabel msdos")
        self.__Excute("parted -s "+dev+" mkpart primary 1 "+size+"GB")
        self.__Excute("mkfs.ext4 "+dev+"1")
        self.__Excute("mount "+dev+"1 /storage")
        writer = open("/etc/fstab","a+")
        writer.write(dev+"1 /storage ext4 rw 0 0\n")
        writer.close()
        return

    def ovf(self,monitor):
        paths = ["046/08c/8a2fdb1e05c44b59bb1dcf4c1b64391b","d21/d55/95ae716d4ef749debf1517650d36c2b9","b22/076/0598b9f0a2ac4f089b4d97f6dfcc5cb6","e55/430/5337ef4cd89042a9a76b3ed8a31fde82","dbe/83c/17b9257d6312419b8c84a4e0c86b2aef"]
        ovfs = ["iop_image_tpl.centos6.7.ovf","iop_image_tpl.centos7.ovf","iop-vm-centos6.5.ovf","iop_vm_centos6.7.ovf","iop_vm_centos7.ovf"]
        for i in range(0, len(ovfs)):
            reader = open("config/ovf/"+ovfs[i],"r")
            lines = reader.readlines()
            reader.close()
            writer = open("resource/iso+package/"+paths[i],"w")
            for line in lines:
                line = line.replace('proxyip',monitor)
                writer.write(line)
            writer.close()
        return

    def init(self,ips,dev):
        self.config(ips,dev)
#        self.mount_disk(dev)
        time.sleep(3)
        print "execute mv -f resource/iso+package/* /storage/"
        self.__Excute("mv -f resource/iso+package/* /storage/")
        self.__Excute("systemctl enable nfs-server.service")
        self.__Excute("systemctl enable rpcbind.service")
        return

class nfs_client():

    __Excute = None

    def __init__(self):
        self.__Excute = os.system
    
    def install(self,ips):
        for ip in ips:
            self.__Excute("ssh "+ip+" yum install -y nfs-utils")
        return

    def run(self,ips):
        for ip in ips:
            self.__Excute("ssh "+ip+" systemctl restart rpcbind.service")

        return

    def config(self,ips):
        local_ip = []
        reader = os.popen("ip addr")
        data = reader.readlines()
        reader.close()
        for d in data:
            if re.match(r"    inet (\d+\.\d+\.\d+\.\d+)/.*$",d):
                x=re.search(r"    inet (\d+\.\d+\.\d+\.\d+)/.*$",d)
                local_ip.append(x.group(1))
        for ip in ips:
            resip = local_ip[0]
            if local_ip[0]=="127.0.0.1":
                resip = local_ip[1]
            self.__Excute("ssh "+ip+" \"mount -t nfs4 "+resip+":/storage /storage\"")
            self.__Excute("ssh "+ip+" \"echo systemctl start rpcbind >> /etc/rc.local;echo mount -t nfs4 "+resip+":/storage /storage >> /etc/rc.local;\"")     #fstab arguments need check

    def init(self,ips):
        for ip in ips:
            self.__Excute("ssh "+ip+" \"if [ ! -x /storage ];then\n mkdir /storage \n fi\"")
        self.config(ips)
       
if __name__ == "__main__":
    T = test()
    ips = T.get_ips()
    server = nfs_server()
    client = nfs_client()
    server.install()
    client.install(ips)
    server.init(ips,dev)
    server.run(ips)
    client.run(ips)
    client.init(ips)


